package org.xxpay.core.common.enumm;

import org.xxpay.core.common.util.I18nUtil;

/**
 * RPC调用返回码枚举类
 * 对应方法调用返回值中的rpcRetCode和rpcRetMsg
 * 支持国际化消息
 * Created by admin on 2016/4/27.
 */
public enum RetEnum {

    // 0000: 成功
    RET_SUCCESS("0000", "operation.success"),

    // 失败(00开始标示通讯层相关错误码)
    RET_REMOTE_UNUSABLE("0001", "error.7003"),
    RET_REMOTE_INVALID("0002", "error.5000"),
    RET_NO_BIZ_SEQUENCE_NO("0003", "error.2000"),
    RET_REMOTE_CHECK_SIGN_FAIL("0004", "error.0013"),
    RET_REMOTE_RPC_SEQ_NO_REPEATED("0005", "error.7004"),
    RET_REMOTE_SIGN_INVALID("0006", "error.0013"),
    RET_REMOTE_DEAL_EXCEPTION("0007", "error.7003"),
    RET_REMOTE_PROTOCOL_INVALID("0008", "error.2001"),
    RET_REMOTE_HTTP_METHOD_INVALID("0009", "error.0011"),

    // 失败(01开始标示参数校验相关错误码)
    RET_PARAM_NOT_FOUND("0101", "error.2000"),
    RET_PARAM_INVALID("0102", "error.2001"),
    RET_PARAM_TOO_LARGE_LIST("0103", "error.2002"),
    RET_PARAM_TYPE_INVALID("0104", "error.2003"),
    RET_CURRENT_PAGE_INVALID("0105", "error.2004"),
    RET_VIEW_NUMBER_INVALID("0106", "error.2005"),
    RET_VIEW_LIMIT_INVALID("0107", "error.2006"),

    //  失败(02开始标示DB操作相关错误码)
    RET_DB_FAIL("0201", "error.3000"),

    // 业务相关
    RET_BIZ_DATA_NOT_EXISTS("1001", "error.1001"),
    RET_BIZ_SING_DATA_FAIL("1002", "error.1002"),
    RET_BIZ_WX_PAY_CREATE_FAIL("1003", "error.1003"),
    RET_BIZ_ALI_PAY_CREATE_FAIL("1004", "error.1004"),
    RET_BIZ_PAY_NOTIFY_VERIFY_FAIL("1005", "error.1005"),
    RET_BIZ_ACCOUNT_NOT_EXIST("1006", "error.1006"),
    RET_BIZ_ACCOUNT_ALLREADY_FROZEN("1007", "error.1007"),
    RET_BIZ_ACCOUNT_CHECKSUM_ERROR("1008", "error.1008"),
    RET_BIZ_ACCOUNT_BALANCE_NOT_ENOUGH("1009", "error.1009"),
    RET_BIZ_ACCOUNT_CANT_TRANSGER_TO_SELF("1010", "error.1010"),
    RET_BIZ_ACCOUNT_USEABLE_BALANCE_NOT_ENOUGH("1011", "error.1011"),


    // 未知错误
    RET_UNKNOWN_ERROR("9999", "error.1000");

    private String code;
    private String messageKey;

    private RetEnum(String code, String messageKey) {
        this.code = code;
        this.messageKey = messageKey;
    }

    /**
     * 获取国际化消息
     * @return 国际化消息
     */
    public String getMessage() {
        return I18nUtil.getMessage(this.messageKey, this.messageKey);
    }

    /**
     * 获取消息键
     * @return 消息键
     */
    public String getMessageKey() {
        return this.messageKey;
    }

    public String getCode()
    {
        return this.code;
    }



    public static RetEnum getRetEnum(String code) {
        if (code == null) {
            return null;
        }

        RetEnum[] values = RetEnum.values();
        for (RetEnum e : values) {
            if (e.getCode().equals(code)) {
                return e;
            }
        }
        return null;
    }
}
